<!--
 * @Description: 商家远程项目编辑模板
 * @Author: liyujie
 * @Date: 2021/08/03
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/03
-->
<template>
    <div class="wrapper">
        <div class="page-header">
            <div class="project-info">
                <div class="info-item">
                    当前项目：{{ ruleForm.project_title }}
                </div>
                <div class="info-item">
                    项目单价：<span style="color: #F95B56">¥{{ ruleForm.task_price }}/个</span>
                </div>
            </div>
            <div class="option-button">
                <div
                    class="button-item"
                    @click="handleRoute('shopProjectList')"
                >
                    取消编辑
                </div>
                <!--<div
                    class="button-item"
                    @click="handlePreview"
                >
                    预览效果
                </div>-->
                <div
                    class="button-item save"
                    @click="handleSubmit"
                >
                    保存
                </div>
            </div>
        </div>
        <div class="content-wrapper">
            <div class="select-wrapper">
                <div class="title">任务选填模块</div>
                <div class="tools-view">
                    <div
                        v-for="(item,index) in toolsList"
                        :key="index"
                        class="tools-item"
                        :class="item.show ? 'focus' : ''"
                    >
                        <div class="tools-title">
                            <i
                                class="iconfont left-icon"
                                :class="item.leftIcon"
                            />
                            <div class="text">{{ item.title }}</div>
                        </div>
                        <el-switch
                            v-model="item.show"
                            @change="changeToolsShow($event, item)"
                        />
                    </div>
                </div>
            </div>
            <div class="form-wrapper">
                <div class="form-header">
                    项目模版<span class="title-tips">(此内容在商家端展示，由商家进行编辑模版的内容)</span>
                </div>
                <div class="form-content">
                    <el-form
                        :model="ruleForm"
                        :rules="rules"
                        ref="ruleForm"
                        label-position="top"
                    >
                        <el-form-item
                            label="任务标题"
                            class="is-required"
                            style="pointer-events: none"
                        >
                            <el-input
                                v-model="ruleForm.task_title"
                                placeholder="商家输入任务标题"
                                maxlength="12"
                                show-word-limit
                            />
                        </el-form-item>
                        <div class="form-item">
                            <el-form-item
                                label="任务时间"
                                class="is-required"
                            >
                                <el-date-picker
                                    v-model="dateTimeRange"
                                    type="datetimerange"
                                    range-separator="-"
                                    style="width: 100%"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                                    :default-time="['00:00:00', '23:59:59']"
                                    value-format="timestamp"
                                />
                            </el-form-item>
                            <el-form-item
                                label="任务总数"
                                class="is-required"
                            >
                                <el-input
                                    v-model="ruleForm.all_count"
                                    placeholder="商家输入任务总数"
                                />
                            </el-form-item>
                        </div>
                        <div class="form-item">
                            <el-form-item
                                label="领取次数"
                                class="is-required"
                            >
                                <el-radio-group
                                    v-model="ruleForm.accept_limit_count"
                                    class="radio-wrapper"
                                >
                                    <el-radio
                                        :label="1"
                                        class="radio-item"
                                    >
                                        单次领取
                                    </el-radio>
                                    <el-radio
                                        :label="-1"
                                        class="radio-item"
                                    >
                                        重复领取
                                    </el-radio>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item/>
                        </div>

                        <div class="form-title">编辑任务选填模块</div>
                        <div class="sort-view">
                            <draggable
                                class="sort-wrapper"
                                v-model="ruleForm.page_config"
                                handle=".sort-button"
                                v-if="ruleForm.page_config.length"
                            >
                                <div
                                    class="sort-item"
                                    v-for="(item, index) in ruleForm.page_config"
                                    :key="item.type"
                                >
                                    <div class="title-view">
                                        <div class="left-title">
                                            {{ item.title }}
                                        </div>
                                        <div class="right-icon">
                                            <span
                                                class="iconfont el-icon-hand-shanchu sort-button"
                                                @click="deleteSortItem(index, item.type)"
                                            />
                                            <span class="iconfont el-icon-hand-paixu sort-button" />
                                        </div>
                                    </div>
                                    <div class="content-view">
                                        <!--任务介绍-->
                                        <div v-if="item.type === 'task_intro'">
                                            <el-input
                                                v-model="item.value"
                                                type="textarea"
                                                placeholder="商家输入任务介绍"
                                                :autosize="{ minRows: 4}"
                                                maxlength="300"
                                                show-word-limit
                                            />
                                        </div>
                                        <!--验收标准-->
                                        <div v-if="item.type === 'task_standard'">
                                            <el-input
                                                v-model="item.value"
                                                type="textarea"
                                                placeholder="商家输入验收标准"
                                                :autosize="{ minRows: 4}"
                                                maxlength="300"
                                                show-word-limit
                                            />
                                        </div>
                                        <!--步骤引导-->
                                        <div
                                            v-if="item.type === 'guide'"
                                            class="steps-view"
                                        >
                                            <div
                                                class="step-item"
                                                v-for="(item, index) in item.value"
                                                :key="index"
                                            >
                                                <el-form-item style="margin-bottom: 12px">
                                                    <el-input
                                                        v-model="item.content"
                                                        type="textarea"
                                                        placeholder="商家输入步骤引导内容"
                                                        :autosize="{ minRows: 4}"
                                                    />
                                                </el-form-item>
                                                <div
                                                    class="left-title"
                                                    style="margin-top: 24px"
                                                >步骤示意图<span style="color: #999;margin-left: 12px">建议宽度750px，建议大小不超过1MB，支持jpg、jpeg、png；</span></div>
                                                <el-form-item>
                                                    <image-upload-multiple
                                                        v-model="item.images"
                                                        limit-type="MB"
                                                        :limit="1"
                                                    />
                                                </el-form-item>
                                            </div>
                                            <div
                                                class="add-btn"
                                                style="margin-top: -12px"
                                            >
                                                <i class="el-icon-plus"/>
                                                添加步骤
                                            </div>
                                        </div>
                                        <!--常见问题-->
                                        <div
                                            v-if="item.type === 'faq'"
                                            class="question-view"
                                        >
                                            <div
                                                class="question-item"
                                                v-for="(item, index) in item.value"
                                                :key="index"
                                            >
                                                <el-form-item>
                                                    <el-row>
                                                        <el-col :span="24">
                                                            <el-input
                                                                v-model="item.title"
                                                                clearable
                                                                placeholder="请输入常见问题"
                                                            />
                                                        </el-col>
                                                        <!--<el-col :span="1">
                                                            <el-button
                                                                type="text"
                                                                style="margin-left: 12px"
                                                            >删除</el-button>
                                                        </el-col>-->
                                                    </el-row>
                                                </el-form-item>
                                                <el-form-item>
                                                    <el-input
                                                        v-model="item.desc"
                                                        type="textarea"
                                                        placeholder="请输入解决方法"
                                                        :autosize="{ minRows: 4}"
                                                    />
                                                </el-form-item>
                                                <div
                                                    class="add-btn"
                                                    style="margin-top: 0;"
                                                >
                                                    <i class="el-icon-plus"/>
                                                    添加常见问题
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </draggable>
                            <div
                                class="sort-empty"
                                v-else
                            >
                                暂时还没有选填内容，点击左侧“任务选填模块”面板添加对应的类型哦～
                            </div>
                        </div>
                        <div class="form-title">用户反馈模块<span class="form-title__tips">学生完成任务所需要提交的凭证</span></div>
                        <el-form-item>
                            <custom-form
                                class="custom-form"
                                ref="customForm"
                                @send="getFormData"
                            />
                            <div class="empty-data">反馈内容由商家进行添加</div>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import {back} from '@/mixin/back';
import {deepClone} from "@/assets/js/utils";
import draggable from "vuedraggable";
import ImageUploadMultiple from "@/components/common/image-upload-multiple";
import CustomForm from "@/components/student/form";

export default {
    name: 'ShopProjectEdit',
    mixins: [back],
    components: {ImageUploadMultiple, draggable, CustomForm},
    data() {
        return {
            ruleForm: {
                shop_project_no: '',
                // 任务标题
                task_title: '',
                // 任务时间
                start_time: '',
                end_time: '',
                // 任务总数
                all_count: '',
                // 领取次数1单次，-1不限
                accept_limit_count: '',
                // 反馈内容
                submit_config_json: '',
                page_config: []
            },
            rules: {},
            toolsList: [{
                leftIcon: 'el-icon-hand-renwujieshao',
                title: '任务介绍',
                type: 'task_intro',
                show: false,
                value: '',
            },{
                leftIcon: 'el-icon-hand-yanshoubiaozhun',
                title: '验收标准',
                type: 'task_standard',
                show: false,
                value: ''
            },{
                leftIcon: 'el-icon-hand-yindaobuzhou',
                title: '引导步骤',
                type: 'guide',
                show: false,
                value: [{content: '',images: []}]
            },{
                leftIcon: 'el-icon-hand-changjianwenti',
                title: '常见问题',
                type: 'faq',
                show: false,
                value: [{title: '',desc: ''}]
            }],
            dateTimeRange: [],
        };
    },
    computed: {},
    watch: {
        dateTimeRange(value) {
            this.ruleForm.start_time = value ? value[0] / 1000 : '';
            this.ruleForm.end_time = value ? value[1] / 1000 : '';
        }
    },
    filters: {},
    mounted() {
        let query = this.$route.query;

        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '工作'}, {title: '商家远程项目'}, {title: '项目管理列表', path: '/student/shop-project-list'}, {title: '编辑项目'}]);
        this.authResult = this.$route.meta.authResult;
        this.ruleForm.shop_project_no = query.shop_project_no;
        this.getDefaultConfig();
    },

    methods: {
        /**
         * @description 获取数据
         */
        getDefaultConfig() {
            let params = {
                shop_project_no: this.ruleForm.shop_project_no
            };

            this.$post('/student/task_stuagent_shop_project%5Cget', params, res => {
                if (res.code === 1) {
                    let data = res.data.page_config;

                    this.ruleForm.page_config = data.map(item => {
                        return this.toolsList.find(option => {
                            if(option.type === item.type) {
                                option.show = true;
                                return option;
                            }
                        });
                    });
                    this.ruleForm.project_title = res.data.project_title;
                    this.ruleForm.task_price = res.data.task_price;
                } else {
                    this.$notify({
                        title: '提示',
                        message: res.msg,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * 提交
         */
        handleSubmit() {
            let params = deepClone(this.ruleForm);

            params.page_config = JSON.stringify(params.page_config);
            this.$post('/student/task_stuagent_shop_project%5Cset_template', params, resp => {
                if (resp.code === 1) {
                    this.$notify({
                        title: '提示',
                        message: '保存成功',
                        duration: 2000,
                        type: 'success'
                    });
                    this.handleRoute('shopProjectList');
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        duration: 2000,
                        type: 'warning'
                    });
                }
            });
        },

        /**
         * 获取自定义表单数据
         * @param data
         */
        getFormData(data) {
            this.ruleForm.form_config = data;
        },

        /**
         * 显示或者隐藏组件
         * @param status
         * @param option
         */
        changeToolsShow(status, option) {
            if(status) {
                this.ruleForm.page_config.push(option);
            } else {
                let index = this.ruleForm.page_config.findIndex(item => {
                    return item.type === option.type;
                });

                this.ruleForm.page_config.splice(index, 1);
            }
        },

        /**
         * 删除可选项
         * @param index
         * @param type
         */
        deleteSortItem(index, type) {
            this.ruleForm.page_config.splice(index, 1);
            this.toolsList.forEach(item => {
                if(item.type === type) {
                    item.show = false;
                }
            });
        },

        /**
         * 预览
         */
        handlePreview() {

        },

    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;
    .page-header {
        height: 68px;
        background: #FFFFFF;
        box-shadow: 0px -1px 0px 0px #F0F2F5;
        padding: 0 12px 0 32px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .project-info {
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
            display: flex;
            align-items: center;
            .info-item {
                height: 14px;
                &:first-of-type {
                    padding-right: 24px;
                    margin-right: 24px;
                    border-right: 2px solid #E0E6F1;
                }
            }
        }
        .option-button {
            display: flex;
            align-items: center;
            .button-item {
                height: 48px;
                padding: 0 24px;
                border-right: 1px solid #F0F2F5;
                font-size: 14px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #666666;
                display: flex;
                align-items: center;
                justify-content: center;
                cursor: pointer;
                &.save {
                    border-right: 0;
                    color: #1890FF;
                }
                &:hover {
                    background: #F9F9F9;
                    transition: all 0.2s;
                }
            }
        }
    }

    .content-wrapper {
        display: flex;
        height: calc(100% - 92px);
        position: relative;
        margin: 12px;
        box-sizing: border-box;
        .select-wrapper {
            width: 256px;
            background: #fff;
            margin-right: 12px;
            flex-shrink: 0;
            .title {
                height: 64px;
                background: #FFFFFF;
                padding: 0 24px;
                display: flex;
                align-items: center;
                border-bottom: 1px solid #F0F2F7;
                font-size: 16px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
            }
            .tools-view {
                padding: 10px 24px;
                .tools-item {
                    height: 60px;
                    background: #FFFFFF;
                    border-radius: 4px;
                    margin-bottom: 10px;
                    cursor: pointer;
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    padding: 0 12px;
                    font-size: 16px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #999999;
                    .tools-title {
                        display: flex;
                        align-items: center;
                        .text {
                            margin-left: 7px;
                        }
                    }
                    &.focus {
                        color: #333333;
                        .iconfont {
                            color: #20A0FF;
                        }
                    }
                }
            }
        }
        .form-wrapper {
            flex: 1;
            background: #fff;
            display: flex;
            flex-direction: column;
            .form-header {
                height: 64px;
                background: #FFFFFF;
                display: flex;
                align-items: center;
                padding: 0 32px;
                font-size: 16px;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 500;
                color: #333333;
                border-bottom: 1px solid #F0F2F7;
                .title-tips {
                    font-size: 12px;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #999999;
                    margin-left: 12px;
                }
            }
            .form-content {
                flex: 1;
                padding: 32px 62px;
                overflow-y: auto;
                &::-webkit-scrollbar {
                    width: 6px;
                }
                .form-item {
                    display: flex;
                    pointer-events: none;
                    .el-form-item {
                        flex: 1;
                        &:first-of-type {
                            margin-right: 24px;
                        }
                    }
                }
                .form-title {
                    font-size: 16px;
                    font-family: PingFangSC-Medium, PingFang SC;
                    font-weight: 500;
                    color: #333333;
                    margin: 30px 0 24px;
                    &__tips {
                        font-size: 14px;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #999999;
                        margin-left: 16px;
                    }
                }

                .radio-wrapper {
                    width: 100%;
                    height: 40px;
                    background: #FFFFFF;
                    border-radius: 4px;
                    border: 1px solid #DCDFE6;
                    display: flex;
                    align-items: center;
                    &:hover {
                        border-color: #C0C4CC;
                        transition: border-color .2s cubic-bezier(.645,.045,.355,1);
                    }
                    .radio-item {
                        flex: 1;
                        height: 14px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        color: #999;
                        &:first-of-type {
                            border-right: 1px solid #DCDFE6;
                        }
                    }
                }

                .sort-view {
                    .sort-wrapper {
                        .sort-item {
                            background: #FFFFFF;
                            border-radius: 4px;
                            border: 1px solid #DFE4ED;
                            padding: 24px;
                            margin-bottom: 12px;
                        }
                        .content-view {
                            pointer-events: none;
                        }
                        .title-view {
                            margin-bottom: 12px;
                            display: flex;
                            align-items: center;
                            justify-content: space-between;
                            .left-title {
                                font-size: 14px;
                                font-family: PingFangSC-Regular, PingFang SC;
                                font-weight: 400;
                                color: #333333;
                            }
                            .right-icon {
                                font-size: 16px;
                                .el-icon-hand-shanchu {
                                    color: #FA5555;
                                    margin-right: 8px;
                                    cursor: pointer;
                                }
                                .el-icon-hand-paixu {
                                    cursor: move;
                                }
                            }
                        }
                    }
                    .sort-empty {
                        height: 286px;
                        background: #FFFFFF;
                        border-radius: 4px;
                        border: 1px dashed #20A0FF;
                        font-size: 14px;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #999999;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                    }
                    .steps-view {
                        .step-item {
                            border-radius: 2px;
                            margin-bottom: 12px;
                            &__header {
                                height: 44px;
                                background: #F0F0F0;
                                border-radius: 2px 2px 0 0;
                                border-bottom: 1px solid #DCDFE6;
                                display: flex;
                                align-items: center;
                                justify-content: space-between;
                                font-size: 16px;
                                font-family: PingFangSC-Medium, PingFang SC;
                                font-weight: 500;
                                color: #333333;
                                padding: 0 32px;
                                &__title {
                                    position: relative;
                                    &::before {
                                        content: "";
                                        display: block;
                                        position: absolute;
                                        width: 4px;
                                        height: 14px;
                                        background: #FF3030;
                                        left: -12px;
                                        top: 50%;
                                        transform: translateY(-50%);
                                    }
                                }
                            }
                            &__content {
                                padding: 24px 32px;
                                .label-tips {
                                    font-size: 14px;
                                    font-family: PingFangSC-Regular, PingFang SC;
                                    font-weight: 400;
                                    color: #999999;
                                    margin-left: 12px;
                                }
                            }
                        }
                    }
                    .add-btn {
                        height: 36px;
                        background: #FFFFFF;
                        border-radius: 4px;
                        border: 1px dashed #DFE4ED;
                        font-size: 14px;
                        font-family: PingFangSC-Medium, PingFang SC;
                        font-weight: 500;
                        color: #999999;
                        line-height: 14px;
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        cursor: pointer;
                        margin-top: 24px;
                        .el-icon-plus {
                            margin-right: 10px;
                        }
                    }
                }
            }
            .empty-data {
                width: 100%;
                height: 84px;
                background: #FCFCFC;
                border-radius: 2px;
                border: 1px dashed #DFE4ED;
                font-size: 14px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #999999;
                display: flex;
                align-items: center;
                justify-content: center;
            }
        }
    }

    /deep/ .el-form {
        .el-form-item__label {
            line-height: normal;
            margin-bottom: 12px;
            padding-bottom: 0;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
        }
        .is-required {
            .el-form-item__label {
                &:before {
                    content: "*";
                    display: inline-block;
                    color: #F56C6C;
                    margin-right: 4px;
                }
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
    // 自定义表单样式
    /deep/ .custom-form {
        background: transparent;
        pointer-events: none;
        .box {
            background: transparent;
            padding: 0;
        }
        .container_header {
            width: 100%;
            margin-bottom: 24px;
            flex-wrap: nowrap;
            .container_item {
                display: flex;
                flex: 1;
                margin-right: 24px;
                &:last-child {
                    margin-right: 0;
                }
            }
            .container_add {
                display: flex;
                align-items: center;
                justify-content: center;
                font-size: 24px;
                color: #999;
            }
            .container_box {
                flex: 1;
                &:hover {
                    border-color: #20A0FF;
                    .container_title, .container_add {
                        color: #20A0FF;
                    }
                }
            }
        }
        .main {
            width: 100%;
            .main_cont {
                padding: 20px 24px;
                background: #F7F7F7;
                border-radius: 4px;
                border: 1px solid #DCDFE6;
                margin-bottom: 12px;
                .main_checkbox_box, .main_radio_box {
                    align-items: center;
                }
                .main_text, .main_textarea, .main_radio, .main_checbox, .main_image_box, .main_image_ipt {
                    width: 100%;
                    .main_radio_head, .main_radio_list, .main_checkbox_list {
                        width: 100%;
                    }
                    .main_radio_list {
                        margin-left: 0;
                        padding-left: 123px;
                    }
                    .main_checkbox_list {
                        margin-left: 0;
                        padding-left: 60px;
                    }
                }
            }
        }
        .no_data {
            display: none;
            width: 100%;
            height: 84px;
            background: #FCFCFC;
            border-radius: 2px;
            border: 1px dashed #DFE4ED;
            font-size: 14px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
        }
    }
    /deep/ .image-list {
        display: flex;
        align-items: flex-end;

        .tips {
            margin-bottom: 40px;
        }
        .uploader-customer {
            background: #fff!important;
        }
    }
}
</style>
